package com.android.tiango.baba.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.entity.UserModel;
import com.android.tiango.baba.util.calendar.DateArrayAdapter;
import com.android.tiango.baba.util.calendar.DateNumericAdapter;
import com.android.tiango.baba.util.calendar.OnWheelChangedListener;
import com.android.tiango.baba.util.calendar.OnWheelScrollListener;
import com.android.tiango.baba.util.calendar.WheelView;

public class DateAlertUtil {

	public static final String[] constellationArr = { "水瓶座", "双鱼座", "牡羊座", "金牛座", "双子座", "巨蟹座",
			"狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };

	public static final int[] constellationEdgeDay = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22,
			22 };

	private String time_year, time_month, time_day;
	private int yearIndex, monthIndex, dayIndex;
	boolean birthdaySaved;

	void updateDays(Context context, WheelView yearWheel, WheelView monthWheel, WheelView dayWheel) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + yearWheel.getCurrentItem());
		calendar.set(Calendar.MONTH, monthWheel.getCurrentItem());

		int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		dayWheel.setViewAdapter(new DateNumericAdapter(context, 1, maxDays, "%d" + "日"));
		int curDay = Math.min(maxDays, dayWheel.getCurrentItem() + 1);
		dayWheel.setCurrentItem(curDay - 1, true);
	}

	public void timeSelectorBirthday(final Context context, final TextView tv, final UserModel model) {
		Calendar mCalendar = Calendar.getInstance();
		LayoutInflater inflater = LayoutInflater.from(context);
		View birthdaySelector = inflater.inflate(R.layout.layout_birthday, null);
		final WheelView yearWheel = (WheelView) birthdaySelector.findViewById(R.id.year);
		final WheelView monthWheel = (WheelView) birthdaySelector.findViewById(R.id.month);
		final WheelView dayWheel = (WheelView) birthdaySelector.findViewById(R.id.day);

		OnWheelChangedListener mChangedListener = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				updateDays(context, yearWheel, monthWheel, dayWheel);
			}
		};

		final int year = mCalendar.get(Calendar.YEAR);
		yearWheel.setViewAdapter(new DateNumericAdapter(context, year - 100, year, "%d" + "年"));
		yearWheel.setCurrentItem(yearIndex);
		yearWheel.addChangingListener(mChangedListener);

		int month = mCalendar.get(Calendar.MONTH);
		List<String> months = new ArrayList<String>();
		months.add("1月");
		months.add("2月");
		months.add("3月");
		months.add("4月");
		months.add("5月");
		months.add("6月");
		months.add("7月");
		months.add("8月");
		months.add("9月");
		months.add("10月");
		months.add("11月");
		months.add("12月");
		monthWheel.setViewAdapter(new DateArrayAdapter(context, months, month));
		monthWheel.setCurrentItem(monthIndex);
		monthWheel.addChangingListener(mChangedListener);

		updateDays(context, yearWheel, monthWheel, dayWheel);
		dayWheel.setCurrentItem(dayIndex);

		time_year = year + "";
		time_month = (month + 1) + "";
		time_day = (mCalendar.get(Calendar.DAY_OF_MONTH)) + "";
		yearWheel.addScrollingListener(new OnWheelScrollListener() {
			@Override
			public void onScrollingStarted(WheelView wheel) {
			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				time_year = (year - 100 + yearWheel.getCurrentItem()) + "";
			}
		});
		monthWheel.addScrollingListener(new OnWheelScrollListener() {
			@Override
			public void onScrollingStarted(WheelView wheel) {
			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				time_month = (monthWheel.getCurrentItem() + 1) + "";
			}
		});
		dayWheel.addScrollingListener(new OnWheelScrollListener() {
			@Override
			public void onScrollingStarted(WheelView wheel) {
			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				time_day = (dayWheel.getCurrentItem() + 1) + "";
			}
		});
		new AlertDialog.Builder(context).setTitle("请选择生日(年/月/日)").setView(birthdaySelector)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						birthdaySaved = false;
						yearIndex = yearWheel.getCurrentItem();
						monthIndex = monthWheel.getCurrentItem();
						dayIndex = dayWheel.getCurrentItem();
						String month = String.valueOf(monthIndex + 1);
						String day = String.valueOf(dayIndex + 1);

						if (month.length() < 2) {
							month = "0" + month;
						}
						if (day.length() < 2) {
							day = "0" + day;
						}
						String str = (year - 100 + yearIndex) + "-" + month + "-" + day;

						String l = getTime(str);
						model.setBirthday(l);
						Calendar c = Calendar.getInstance();
						c.set(yearIndex, monthIndex, dayIndex);// 指定一个日期
						String s = date2Constellation(c);
						tv.setText(str + "\t" + s);
					}
				}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				}).create().show();

	}

	/**
	 * 根据日期获取星座
	 * 
	 * @param time
	 * @return
	 */
	public String date2Constellation(Calendar time) {
		int month = time.get(Calendar.MONTH);
		int day = time.get(Calendar.DAY_OF_MONTH);
		if (day < constellationEdgeDay[month]) {
			month = month - 1;
		}
		if (month >= 0) {
			return constellationArr[month];
		}
		// default to return 魔羯
		return constellationArr[11];
	}

	// 将时间戳转为字符串
	private String getStrTime(String cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		long lcc_time = Long.valueOf(cc_time);
		re_StrTime = sdf.format(new Date(lcc_time * 1000L));
		return re_StrTime;
	}

	// 将字符串为时间戳转
	public String getTime(String user_time) {
		String str = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d;
		try {
			d = sdf.parse(user_time);
			long l = d.getTime() / 1000;
			str = String.valueOf(l);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return str;
	}

	public String getDateStr(String s) {
		String birth = getStrTime(s);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = sdf.parse(birth);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			return birth + "\t" + date2Constellation(calendar);
		} catch (Exception e) {
			return "";
		}

	}

}
